<template>
  <el-card style="max-width: 100%">
    <template #header>
      <div class="card-header">
        <span>今天安排，明明白白</span>
        <el-button :icon="Plus" plain type="primary" @click="todoStore.preAddNewItem()"
          >添加新任务</el-button
        >
      </div>
    </template>
    <!-- 任务过滤 -->
    <todo-radio></todo-radio>
    <!-- Todo Table -->

    <hr />
    <el-table :data="filteredTodos" stripe style="width: 100%">
      <el-table-column prop="todoid" label="任何编号" width="80" />
      <el-table-column prop="title" label="任务题目" />
      <el-table-column prop="fulltime" label="上报时间" />
      <el-table-column prop="todotime" label="计划执行时间" />
      <el-table-column prop="benintime" label="开始执行" />
      <el-table-column prop="endtime" label="完成时间" />
      <el-table-column prop="status" label="状态" />
      <el-table-column label="Operations" width="350">
        <template #default="scope">
          <el-button size="small" @click="handleStart(scope.$index, scope.row)"> START </el-button>
          <el-button size="small" @click="handleFinish(scope.$index, scope.row)">
            FINISH
          </el-button>
          <el-button size="small" type="danger" @click="handleEdit(scope.$index, scope.row)">
            Edit
          </el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">
            Delete
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>Footer content</template>
  </el-card>

  <!-- 挂载TodoDialog ,但是默认是隐藏的 -->
  <todo-dialog></todo-dialog>
</template>

<script setup>
// 挂载TodoDialog
import TodoRadio from './TodoRadio.vue'
import TodoDialog from './TodoDialog.vue'

import { storeToRefs } from 'pinia'
import useTodoStore from '../../../stores/todo.js'
import { Plus } from '@element-plus/icons-vue'
import { onMounted } from 'vue'

const handleEdit = (index, row) => {
  console.log(index, row)
  todoStore.preEditItem(index)
}

const handleFinish = (index, row) => {
  console.log(index, row)
  todoStore.finishItem(index)
}
const handleDelete = (index, row) => {
  console.log(index, row)
  todoStore.deleteItem(index)
}
const handleStart = (index, row) => {
  todoStore.startItem(index)
}

const todoStore = useTodoStore()
const { filteredTodos } = storeToRefs(todoStore)

onMounted(() => {
  todoStore.findAll()
})
</script>
<style lang="scss" scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  justify-items: center;
}
</style>
